﻿<%@ Page Language="C#" MasterPageFile="~/Addmin/BackEnd/MTP_Admin.Master" AutoEventWireup="true" CodeBehind="Order_Detail.aspx.cs" Inherits="Bussiness.View.BackEnd.Order_Detail" %>
<%@ Register Src="~/Addmin/BackEnd/User_Control/TK_SanPham.ascx" TagName="TKSP" TagPrefix="uc2" %>
<%@ Register Src="~/Addmin/BackEnd/User_Control/Size.ascx" TagName="TKSIZE" TagPrefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                        <ext:Panel ID="Window1" runat="server" Title="Chi tiết hóa đơn" Icon="Application"
                            Height="500" PaddingSummary="10px 5px 0 5px" >
                            <Items>
                                <ext:FieldSet ID="FieldSet2" runat="server" Layout="ColumnLayout" Title="Thông tin chung"
                                    PaddingSummary="10px 5px 0 5px" Height="100">
                                    <Items>
                                        <ext:Container ID="Container1" runat="server" LabelAlign="Right" Layout="FormLayout"
                                            ColumnWidth=".5">
                                            <Items>
                                                <ext:Hidden ID="pu_ID" runat="server" IDMode="Static" />
                                                <ext:Hidden ID="total_price_import" runat="server" IDMode="Static" />
                                                <ext:TextField ID="pu_tenKH" runat="server" FieldLabel="Tên khách hàng" AnchorHorizontal="100%"
                                                    LabelAlign="Right" IDMode="Static" />
                                            </Items>
                                            <Items>
                                                <ext:TextField ID="pu_soDT" runat="server" FieldLabel="Số điện thoại" AnchorHorizontal="100%"
                                                    LabelAlign="Right" IDMode="Static" />
                                            </Items>
                                        </ext:Container>
                                        <ext:Container ID="Container4" runat="server" LabelAlign="Right" Layout="FormLayout"
                                            ColumnWidth=".5">
                                            <Items>
                                                <ext:TextField ID="pu_diaChi" runat="server" FieldLabel="Địa chỉ" AnchorHorizontal="100%"
                                                    LabelAlign="Right" IDMode="Static" />
                                            </Items>
                                            <Items>
                                                <ext:DateField runat="server" ID="pu_ngayBan" IDMode="Static" FieldLabel="Ngày bán"
                                                    AnchorHorizontal="100%" />
                                            </Items>
                                        </ext:Container>
                                    </Items>
                                </ext:FieldSet>
                            </Items>
                            <Items>
                                <ext:GridPanel ID="DS_SanPham_Grid" IDMode="Static" runat="server" Height="200" AutoScroll="true"
                                    Region="Center">
                                    <Store>
                                        <ext:Store ID="DS_SanPham_Store" runat="server" AutoDataBind="true" SkipIdForNewRecords="false">
                                            <Reader>
                                                <ext:ArrayReader >
                                                    <Fields>
                                                        <ext:RecordField Name="ID" Type="Int" Mapping="ID" />
                                                        <ext:RecordField Name="NameProduct" Type="String" Mapping="NameProduct" />
                                                        <ext:RecordField Name="CodeProduct" Type="String" Mapping="CodeProduct" />
                                                        <ext:RecordField Name="Unit" Type="String" Mapping="Unit" />
                                                        <ext:RecordField Name="SinglePrice" Type="Int" Mapping="SinglePrice" />
                                                        <ext:RecordField Name="Quantity" Type="Int" Mapping="Quantity" />
                                                        <ext:RecordField Name="TotalPrice" Type="Int" Mapping="TotalPrice" />
                                                        <ext:RecordField Name="HaveSize" Type="Boolean" Mapping="HaveSize" />
                                                        <ext:RecordField Name="Size" Type="String" Mapping="Size" />
                                                        <ext:RecordField Name="ID_Size" Type="Int" Mapping="ID_Size" />
                                                        <ext:RecordField Name="SingleImportPrice" Type="Int" Mapping="SingleImportPrice" />
                                                        <ext:RecordField Name="TotalsingleImportPrice" Type="Int" Mapping="TotalsingleImportPrice" />
                                                    </Fields>
                                                </ext:ArrayReader>
                                            </Reader>
                                        </ext:Store>
                                    </Store>
                                    <ColumnModel ID="ColumnModel1" runat="server" AutoDataBind="true">
                                        <Columns>
                                            <ext:RowNumbererColumn />
                                            
                                            <ext:Column ColumnID="ID" DataIndex="ID" Hidden="true" />
                                            <ext:Column ColumnID="ID_Size" DataIndex="ID_Size" Hidden="true" />
                                            <ext:Column ColumnID="SingleImportPrice" DataIndex="SingleImportPrice" Hidden="true" />
                                            <ext:Column ColumnID="TotalsingleImportPrice" DataIndex="TotalsingleImportPrice" Hidden="true" /> 
                                            <ext:Column ColumnID="HaveSize" DataIndex="HaveSize" Hidden="true" />
                                            <ext:Column ColumnID="FactSizeCount" DataIndex="FactSizeCount" Hidden="true" />
                                            <ext:Column Header="Tên sản phẩm" DataIndex="NameProduct" AutoDataBind="true" Width="100"
                                                MenuDisabled="True" />
                                            <ext:Column Header="Mã sản phẩm" DataIndex="CodeProduct" AutoDataBind="true" Width="90" />
                                            <ext:Column Header="Đơn vị" DataIndex="Unit" AutoDataBind="true" Width="90" />
                                            <ext:NumberColumn Header="Đơn giá" DataIndex="SinglePrice" AutoDataBind="true" Width="90"
                                                MenuDisabled="True" Format="0,00">
                                                <Editor>
                                                    <ext:NumberField ID="editor_SinglePrice" AllowBlank="true" runat="server">
                                                    </ext:NumberField>                                                    
                                                </Editor>
                                            </ext:NumberColumn>
                                            <ext:NumberColumn Header="Số lượng" DataIndex="Quantity" AutoDataBind="true" Width="70"
                                                MenuDisabled="True" Format="0">
                                                <Editor>
                                                    <ext:NumberField ID="editor_SL" AllowBlank="true" runat="server">
                                                    </ext:NumberField>
                                                </Editor>
                                            </ext:NumberColumn>
                                            <ext:Column Header="Size" DataIndex="Size" AutoDataBind="true" Width="90" />
                                            <ext:NumberColumn Header="Tổng tiền" DataIndex="TotalPrice" AutoDataBind="true" MenuDisabled="True"
                                                Format="0,00" />
                                                <ext:CommandColumn Header="Chọn Size" >
                                                <Commands >
                                                    <ext:GridCommand Icon="Add" CommandName="SelectSize" ToolTip-Text="Chọn Size" >
                                                        
                                                    </ext:GridCommand>                                                 
                                                </Commands>
                                            </ext:CommandColumn>
                                        </Columns>
                                    </ColumnModel>
                                    <SelectionModel>
                                        <ext:RowSelectionModel runat="server" ID="RowSelectionModel2">
                                        </ext:RowSelectionModel>
                                    </SelectionModel>
                                    <Listeners>
                                        <AfterEdit Fn="Caculate" />
                                         <Command Handler="detail(record,command, record.data.ID)" />
                                    </Listeners>
                                    <Buttons>
                                        <ext:Button runat="server" Text="Thêm SP" ID="Button4" Icon="Add">
                                            <Listeners>
                                                <Click Fn="show_SP" />
                                            </Listeners>
                                        </ext:Button>
                                        <ext:Button runat="server" Text="Xóa" ID="Button5" Icon="Delete">
                                            <Listeners>
                                                <Click Handler="#{DS_SanPham_Grid}.deleteSelected();Caculate()"/>
                                            </Listeners>
                                        </ext:Button>
                                    </Buttons>
                                </ext:GridPanel>
                            </Items>
                            <Items>
                                <ext:FieldSet ID="FieldSet3" runat="server" Layout="ColumnLayout" Border="false"
                                    PaddingSummary="10px 5px 0 5px" Height="50">
                                    <Items>
                                        <ext:Container ID="Container5" runat="server" LabelAlign="Right" Layout="FormLayout"
                                            ColumnWidth=".5">
                                            <Items>
                                                <ext:NumberField ID="pu_soLuongSP" IDMode="Static" runat="server" FieldLabel="Số lượng SP" />
                                            </Items>
                                        </ext:Container>
                                        <ext:Container ID="Container6" runat="server" LabelAlign="Right" Layout="FormLayout"
                                            ColumnWidth=".5">
                                            <Items>
                                                <ext:NumberField ID="pu_tongTien" IDMode="Static" runat="server" FieldLabel="Tổng tiền" />
                                            </Items>
                                        </ext:Container>
                                    </Items>
                                </ext:FieldSet>
                                <ext:Panel ID="Panel3" runat="server" Layout="ColumnLayout" PaddingSummary="15 3 3 3"
                                    Border="false" LabelWidth="250">
                                    <Items>
                                        <ext:Label runat="server" ID="tienChu" FieldLabel="Số tiền bằng chữ(Amount in word)"
                                            IDMode="Static" AnchorHorizontal="100%">
                                        </ext:Label>
                                    </Items>
                                </ext:Panel>
                            </Items>
                            <Buttons>
                                <ext:Button runat="server" Text="Lưu hóa đơn" ID="Button2121" Icon="Add">
                                    <DirectEvents>
                                        <Click OnEvent="save_Order" Before="validateGrid">
                                            <ExtraParams>
                                                <ext:Parameter Name="valueGrid" Value="Ext.encode(#{DS_SanPham_Grid}.getRowsValues({selectedOnly : false}))"
                                                    Mode="Raw" />
                                            </ExtraParams>
                                        </Click>
                                    </DirectEvents>
                                </ext:Button>
                                
                            </Buttons>
                        </ext:Panel>

        <uc2:TKSP ID="TK_SP" runat="server" />
        <uc2:TKSIZE ID="TKSP1" runat="server" />
        


<script type="text/javascript">


    function show_SP() {
        Ext.getCmp('tk_SP_Grid').getSelectionModel().clearSelections(true);
        Ext.getCmp('tk_SP_Grid').getView().refresh();
        Ext.getCmp('tk_SanPham').setVisible(true);
    }
    function close_SP() {
        Ext.getCmp('tk_SanPham').setVisible(false);
    }
    function detail(record,command, id) {
        var store = Ext.getCmp('DS_SanPham_Grid').getStore();
        Ext.getCmp('DS_SanPham_Grid').getSelectionModel().selectRow(store.indexOf(record));
        Ext.getCmp('DS_SanPham_Grid').getView().refresh();
        Ext.net.DirectMethod.request({
                    url: "Order_Detail.aspx",
                    cleanRequest: true,
                    params: {
                        Action: command,
                        ID: id
                    }
                });
    }

    function validateGrid() {
        var store = Ext.getCmp('DS_SanPham_Grid').getStore();
        var check = true;


        if (store.getCount() == 0) {
            Ext.Msg.alert('Lỗi', 'Bạn phải chọn ít nhất một sản phẩm');
            check = false;
        }
        store.each(function (record) {
            if (record.data['HaveSize'] == true && record.data['Size'] == "") {
                Ext.Msg.alert('Lỗi', 'Bạn phải chọn Size cho sản phẩm ' + record.data['NameProduct']);
                check = false;
            }
            if (record.data['Quantity'] == '' || record.data['Quantity'] == null) {
                Ext.Msg.alert('Lỗi', 'Bạn phải nhập số lượng sản phẩm');
                check = false;
            }
        });
        if (check == false) return false;
        
    }

    function checkSizeCount() {
        var record = Ext.getCmp('DS_SanPham_Grid').getSelectionModel().getSelected();
        if (Number(record.data['Quantity']) > Number(record.data['FactSizeCount'])) {
            Ext.Msg.alert('Lỗi', 'Bạn chỉ có thể chọn tối đa ' + Number(record.data['FactSizeCount']) + ' sản phẩm ' + record.data['NameProduct']);
            record.data['Quantity'] = " ";
            Ext.getCmp('DS_SanPham_Grid').getView().refresh();
            return false;
        } else return true;
    }

    function Caculate() {
       // if (checkSizeCount() == true) {

            var store = Ext.getCmp('DS_SanPham_Grid').getStore();
            var soluong = 0;
            var tongtien = 0;
            var tongtiennhap = 0;
            store.each(function(record) {
                if (record.data['SinglePrice'] != null || record.data['SinglePrice'] != 0)
                    record.data['TotalPrice'] = record.data['SinglePrice'] * record.data['Quantity'];

                if (record.data['SingleImportPrice'] != null || record.data['SingleImportPrice'] != 0)
                    record.data['TotalsingleImportPrice'] = record.data['SingleImportPrice'] * record.data['Quantity'];

                soluong += record.data['Quantity'];
                tongtien += record.data['TotalPrice'];
                tongtiennhap += record.data['TotalsingleImportPrice'];
            });

            Ext.getCmp('pu_soLuongSP').setValue(soluong);
            Ext.getCmp('pu_tongTien').setValue(tongtien);
            Ext.getCmp('total_price_import').setValue(tongtiennhap);

            var str2 = "";
            str2 = tongtien.toString().replace(',', '');
            Ext.getCmp('tienChu').setText(DocTienBangChu(Number(str2)))
            Ext.getCmp('DS_SanPham_Grid').getView().refresh();


            

      //  }
    }

    window.onfinish = function() {
        Caculate()
    }
    //doc vao mot so va tra ve chu cua so do
    function doiSoThanhChu(so) {
        var result = "";
        switch (so) {
            case "0": result = " "; break;
            case "1": result = " Một"; break;
            case "2": result = " Hai"; break;
            case "3": result = " Ba"; break;
            case "4": result = " Bốn"; break;
            case "5": result = " Năm"; break;
            case "6": result = " Sáu"; break;
            case "7": result = " Bảy"; break;
            case "8": result = " Tám"; break;
            case "9": result = " Chín"; break;
        }


        return result;
    }


    var ChuSo = new Array(" không ", " một ", " hai ", " ba ", " bốn ", " năm ", " sáu ", " bảy ", " tám ", " chín ");
    var Tien = new Array("", " nghìn", " triệu", " tỷ", " nghìn tỷ", " triệu tỷ");

    //1. Hàm đọc số có ba chữ số;
    function DocSo3ChuSo(baso) {
        var tram;
        var chuc;
        var donvi;
        var KetQua = "";
        tram = parseInt(baso / 100);
        chuc = parseInt((baso % 100) / 10);
        donvi = baso % 10;
        if (tram == 0 && chuc == 0 && donvi == 0) return "";
        if (tram != 0) {
            KetQua += ChuSo[tram] + " trăm ";
            if ((chuc == 0) && (donvi != 0)) KetQua += " linh ";
        }
        if ((chuc != 0) && (chuc != 1)) {
            KetQua += ChuSo[chuc] + " mươi";
            if ((chuc == 0) && (donvi != 0)) KetQua = KetQua + " linh ";
        }
        if (chuc == 1) KetQua += " mười ";
        switch (donvi) {
            case 1:
                if ((chuc != 0) && (chuc != 1)) {
                    KetQua += " mốt ";
                }
                else {
                    KetQua += ChuSo[donvi];
                }
                break;
            case 5:
                if (chuc == 0) {
                    KetQua += ChuSo[donvi];
                }
                else {
                    KetQua += " lăm ";
                }
                break;
            default:
                if (donvi != 0) {
                    KetQua += ChuSo[donvi];
                }
                break;
        }
        return KetQua;
    }

    //2. Hàm đọc số thành chữ (Sử dụng hàm đọc số có ba chữ số)

    function DocTienBangChu(SoTien) {
        var lan = 0;
        var i = 0;
        var so = 0;
        var KetQua = "";
        var tmp = "";
        var ViTri = new Array();
        if (SoTien < 0) return "Số tiền âm !";
        if (SoTien == 0) return "Không đồng !";
        if (SoTien > 0) {
            so = SoTien;
        }
        else {
            so = -SoTien;
        }
        if (SoTien > 8999999999999999) {
            //SoTien = 0;
            return "Số quá lớn!";
        }
        ViTri[5] = Math.floor(so / 1000000000000000);
        if (isNaN(ViTri[5]))
            ViTri[5] = "0";
        so = so - parseFloat(ViTri[5].toString()) * 1000000000000000;
        ViTri[4] = Math.floor(so / 1000000000000);
        if (isNaN(ViTri[4]))
            ViTri[4] = "0";
        so = so - parseFloat(ViTri[4].toString()) * 1000000000000;
        ViTri[3] = Math.floor(so / 1000000000);
        if (isNaN(ViTri[3]))
            ViTri[3] = "0";
        so = so - parseFloat(ViTri[3].toString()) * 1000000000;
        ViTri[2] = parseInt(so / 1000000);
        if (isNaN(ViTri[2]))
            ViTri[2] = "0";
        ViTri[1] = parseInt((so % 1000000) / 1000);
        if (isNaN(ViTri[1]))
            ViTri[1] = "0";
        ViTri[0] = parseInt(so % 1000);
        if (isNaN(ViTri[0]))
            ViTri[0] = "0";
        if (ViTri[5] > 0) {
            lan = 5;
        }
        else if (ViTri[4] > 0) {
            lan = 4;
        }
        else if (ViTri[3] > 0) {
            lan = 3;
        }
        else if (ViTri[2] > 0) {
            lan = 2;
        }
        else if (ViTri[1] > 0) {
            lan = 1;
        }
        else {
            lan = 0;
        }
        for (i = lan; i >= 0; i--) {
            tmp = DocSo3ChuSo(ViTri[i]);
            KetQua += tmp;
            if (ViTri[i] > 0) KetQua += Tien[i];
            if ((i > 0) && (tmp.length > 0)) KetQua += ','; //&& (!string.IsNullOrEmpty(tmp))
        }
        if (KetQua.substring(KetQua.length - 1) == ',') {
            KetQua = KetQua.substring(0, KetQua.length - 1);
        }
        KetQua = KetQua.substring(1, 2).toUpperCase() + KetQua.substring(2) + " đồng";
        return KetQua; //.substring(0, 1);//.toUpperCase();// + KetQua.substring(1);

    }
</script>

</asp:Content>